package br.com.main.client.system.botoes;

import java.util.ArrayList;
import java.util.List;

import br.com.main.client.system.botoes.MoradoresPresenter.Display;

import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SingleSelectionModel;

public class MoradoresView extends Composite implements Display {

	private HorizontalPanel panelMain;
	private VerticalPanel panelLeft;
	private VerticalPanel panelRight;
	private TextBox tbPesquisar;
	private List<Morador> moradores;
	private CellTable<Morador> tabela;
	private SingleSelectionModel<Morador> selectionModel;

	private VerticalPanel panelCadastroMorador;
	private Label lbNovoMorador;
	private TextBox tbNome;
	private TextBox tbCpf;
	private TextBox tbEmail;
	private TextBox tbTel;
	private TextBox tbData_Nasc;
	private TextBox tbNumero;
	private TextBox tbBloco;
	private TextBox tbAndar;
	private HorizontalPanel pnBotoes;
	private Button btEditar;
	private Button btExcluir;
	private Button btSalvar;

	public MoradoresView() {
		init();
		initWidget(panelMain);
	}

	private void criarTabela() {
		TextColumn<Morador> nomeColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getNome();
			}
		};
		tabela.addColumn(nomeColumn, "Nome");

		TextColumn<Morador> cpfColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getCpf();
			}
		};
		tabela.addColumn(cpfColumn, "Cpf");

		TextColumn<Morador> apColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getEmail();
			}
		};
		tabela.addColumn(apColumn, "Email");

		TextColumn<Morador> telColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getTelefone();
			}
		};
		tabela.addColumn(telColumn, "Telefone");

		TextColumn<Morador> dataColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getData_nasc();
			}
		};
		tabela.addColumn(dataColumn, "Data Nascimento");

		TextColumn<Morador> numeroColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getNumero();
			}
		};
		tabela.addColumn(numeroColumn, "Número");

		TextColumn<Morador> blocoColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getBloco();
			}
		};
		tabela.addColumn(blocoColumn, "Bloco");

		TextColumn<Morador> andarColumn = new TextColumn<Morador>() {

			@Override
			public String getValue(Morador object) {
				return object.getAndar();
			}
		};
		tabela.addColumn(andarColumn, "Andar");
	}

	private void init() {
		panelMain = new HorizontalPanel();
		panelMain.setSpacing(25);
		panelMain.setBorderWidth(1);

		panelLeft = new VerticalPanel();
		panelRight = new VerticalPanel();
		tbPesquisar = new TextBox();
		moradores = new ArrayList<Morador>();
		tabela = new CellTable<Morador>();
		tabela.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		criarTabela();

		selectionModel = new SingleSelectionModel<Morador>();
		tabela.setSelectionModel(selectionModel);

		tbNome = new TextBox();
		tbCpf = new TextBox();
		tbEmail = new TextBox();
		tbTel = new TextBox();
		tbData_Nasc = new TextBox();
		tbNumero = new TextBox();
		tbBloco = new TextBox();
		tbAndar = new TextBox();
		setEnabledCampos(false);
		btEditar = new Button("Editar");
		btExcluir = new Button("Excluir");
		btSalvar = new Button("Salvar");
		pnBotoes = new HorizontalPanel();
		pnBotoes.setSpacing(5);
		pnBotoes.add(btEditar);
		pnBotoes.add(btExcluir);
		pnBotoes.add(btSalvar);

		panelCadastroMorador = new VerticalPanel();
		panelCadastroMorador.setBorderWidth(1);
		panelCadastroMorador.setSpacing(5);
		HorizontalPanel hp = new HorizontalPanel();
		lbNovoMorador = new Label("Cadastro Morador [ + ]");
		lbNovoMorador.addStyleName("labelPointer");
		panelCadastroMorador.add(lbNovoMorador);
		panelCadastroMorador.add(hp);
		panelCadastroMorador.add(new Label("Nome"));
		panelCadastroMorador.add(tbNome);
		panelCadastroMorador.add(new Label("Cpf"));
		panelCadastroMorador.add(tbCpf);
		panelCadastroMorador.add(new Label("Email"));
		panelCadastroMorador.add(tbEmail);
		panelCadastroMorador.add(new Label("Telefone"));
		panelCadastroMorador.add(tbTel);
		panelCadastroMorador.add(new Label("Data Nascimento"));
		panelCadastroMorador.add(tbData_Nasc);
		panelCadastroMorador.add(new Label("Informações do Apartamento"));
		panelCadastroMorador.add(new Label("Numero"));
		panelCadastroMorador.add(tbNumero);
		panelCadastroMorador.add(new Label("Bloco"));
		panelCadastroMorador.add(tbBloco);
		panelCadastroMorador.add(new Label("Andar"));
		panelCadastroMorador.add(tbAndar);
		panelCadastroMorador.add(pnBotoes);
		panelCadastroMorador.setVisible(true);

		panelLeft.setBorderWidth(1);
		panelLeft.setSpacing(5);
		panelLeft.add(new Label("Pesquisar "));
		panelLeft.add(tbPesquisar);
		panelLeft.add(new Label("Lista dos Moradores"));
		panelLeft.add(tabela);

		panelRight.add(panelCadastroMorador);

		panelMain.add(panelLeft);
		panelMain.add(panelRight);
	}
	
	@Override
	public Button getBtExcluir() {
		return btExcluir;
	}
	
	@Override
	public void addNovoMorador(Morador e) {
		moradores.add(e);
	}

	@Override
	public List<Morador> getList() {
		return moradores;
	}

	@Override
	public void atualizaTabela() {
		tabela.setRowCount(moradores.size(), true);
		tabela.setRowData(0, moradores);
	}

	@Override
	public TextBox getTbNome() {
		return tbNome;
	}

	@Override
	public TextBox getTbCpf() {
		return tbCpf;
	}

	@Override
	public TextBox getTbTel() {
		return tbTel;
	}
	
	@Override
	public TextBox getTbData_nasc() {
		return tbData_Nasc;
	}
	
	@Override
	public TextBox getTbNumero() {
		return tbNumero;
	}
	
	@Override
	public TextBox getTbBloco() {
		return tbBloco;
	}
	
	@Override
	public TextBox getTbAndar() {
		return tbAndar;
	}

	@Override
	public void limparCampos() {
		tbNome.setText("");
		tbCpf.setText("");
		tbEmail.setText("");
		tbTel.setText("");
		tbData_Nasc.setText("");
		tbNumero.setText("");
		tbBloco.setText("");
		tbAndar.setText("");
	}

	@Override
	public SingleSelectionModel<Morador> getSelectionModel() {
		return selectionModel;
	}

	@Override
	public Button getBtEditar() {
		return btEditar;
	}

	@Override
	public void setEnabledCampos(boolean b) {
		tbNome.setEnabled(b);
		tbCpf.setEnabled(b);
		tbEmail.setEnabled(b);
		tbTel.setEnabled(b);
		tbData_Nasc.setEnabled(b);
		tbNumero.setEnabled(b);
		tbBloco.setEnabled(b);
		tbAndar.setEnabled(b);
	}

	@Override
	public Label getLbNovoMorador() {
		return lbNovoMorador;
	}

	@Override
	public TextBox getTbEmail() {
		return tbEmail;
	}

	@Override
	public Button getBtSalvar() {
		return btSalvar;
	}

}